//index.js
//获取应用实例
var app = getApp();
var config = require('../../utils/config');
var url = config.url;
var util = require('../../utils/util');
var db = require('../../utils/db');

Page({
  data: {
    bookList: [],
    inputValue: '',
    isSearch:false
  },
  inputChange: function (e) {
    this.data.inputValue = e.detail.value;
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    //console.log('onload index')
    var that = this;
    // 微信登陆
    // app.getUserInfo(function(userInfo){
    //   console.log('微信登陆接口调用')
    //   console.log(userInfo)
    // })

    // wx.login({
    //   success (res) {
    //     if (res.code) {
    //       // console.log(res)
    //       // 发起网络请求
    //       wx.request({
    //         url: 'https://test.com/onLogin',
    //         data: {
    //           code: res.code
    //         }
    //       })
    //     } else {
    //       console.log('登录失败！' + res.errMsg)
    //     }
    //   }
    // })


  },
  queryBooks: function (e) {
    var that = this;

    var inputMsg = that.data.inputValue;
    var options = {
      url: config.clubApi.list,
      data: {
        appkey: config.appKey,
        type: 'bookLibrary',
        // columns:'title',
        keywords: inputMsg
        //columns: ['id', 'isbn13', 'title']
      }
    };

    util.request(options, (res, err) => {
      if(res.data.result.length == 0){
        that.setData({
          bookList: [],
          isSearch:true
        });
      }else{
        var books = [];
        for (var i = 0; i < res.data.result.length; i++) {
          books.push(res.data.result[i]);
        }
        that.setData({
          bookList: books,
          isSearch:false
        });
      }
     
    });

  },
  goToDetailPage: function (e) {

    var isbn13 = e.currentTarget.id;
    var qty = e.currentTarget.dataset.qty;
    wx.navigateTo({
      url: '../detail/detail?id=' + isbn13 + '&qty=' + qty
    });

  },
  onShow: function () {
    // 页面显示
    //console.log('onshow');
    this.queryAllBooks();
    // this.queryBooks();
  },
  queryAllBooks: function () {

    var that = this;
    var options = {
      url: config.clubApi.list,
      data: {
        appkey: config.appKey,
        type: 'bookLibrary'
      }
    };

    util.request(options, function (res) {
      var books = [];
      for (var i = 0; i < res.data.result.length; i++) {
         books.push(res.data.result[i]);
      }
      that.setData({
        bookList: books
      })
    });
    


  }

})
